package com.zzb.demo.service.impl;

import com.zzb.demo.mapper.StudentMapper;
import com.zzb.demo.model.domain.Student;
import com.zzb.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zzb
 * @Date 2024/12/13 10:51
 * @Version 1.0
 * @description
 */

public class StudentServiceImpl implements StudentService {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Student> getStudentsByCondition(Student student) {
        String sql = "SELECT * FROM student WHERE id = ?";
        return jdbcTemplate.query(sql, new Object[]{student.getId()}, (rs, rowNum) -> {
            Student s = new Student();
            s.setId(rs.getInt("id"));
            s.setName(rs.getString("name"));
            s.setAge(rs.getInt("age"));
            s.setCid(rs.getString("cid"));
            return s;
        });
    }

    @Override
    public int updateStudent(Student student) {
        String sql = "UPDATE student SET name = ?, age = ?, cid = ? WHERE id = ?";
        return jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getCid(), student.getId());
    }

    @Override
    public int insertStudent(Student student) {
        String sql = "INSERT INTO student (id, name, age, cid) VALUES (?, ?, ?, ?)";
        return jdbcTemplate.update(sql, student.getId(), student.getName(), student.getAge(), student.getCid());
    }

    @Override
    public int delStudent(Student student) {
        String sql = "DELETE FROM student WHERE id = ?";
        return jdbcTemplate.update(sql, student.getId());
    }
}
